<?php
class User_UseraccountController extends Custom_Controller_Action
{
    
    public function init()
    {   
        $this->_session	= Zend_Registry::get('mySession');
        //echo '<pre>';print_r($this->_session);die();
        self::_setTab();
    
    }
    
    public function generalAction()
    {
        $this->_layout = Zend_Layout::getMvcInstance();
        $this->_layout->setLayout('layout');        
        $userid=1;
    	$form = new User_Form_General();
    	$this->view->generalform = $form;  
    }
    
    public function indexAction()
    {
        $this->_layout = Zend_Layout::getMvcInstance();
        $this->_layout->setLayout('layout');
        $userid=1;
    	$userObj = new User_Model_DbTable_User();
    	$userinfo = $userObj->getOne($userid);  

    	$Gform = new User_Form_General($userid);
    	$Gform->populate($userinfo);
        $this->view->generalform = $Gform;
        if($this->getRequest()->isPost())
        {   $formData = $this->getRequest()->getPost();
            if($Gform->isValid($formData))
            {
                $values = $Gform->getValues();
                //if(empty($values['image'])) unset ($values['image']);
                $res = $userObj->updateById($values, $userid);

                if($res)
                {
                    $this->addMessage(" Information updated successfully");
                    $this->RedirectUrl("user/dashboard");
                }
                else
                {
                    $this->addMessage(" Information not updated successfully", 'error');
                }
            }  
        }
        
    	$user = new User_Model_DbTable_UserPersonal();
    	$userinfo = $user->getOne($userid);        
    	$Pform = new User_Form_Personal();
    	$this->view->personalform = $Pform;

    	$user = new User_Model_DbTable_UserEducation();
    	$userinfo = $user->getOne($userid);        
     	$Eform = new User_Form_Education();
    	$this->view->educationform = $Eform; 

    	$user = new User_Model_DbTable_UserInterest();
    	$userinfo = $user->getOne($userid);        
    	$Iform = new User_Form_Interests();
    	$this->view->interestsform = $Iform;  

    	$user = new User_Model_DbTable_UserPrivacy();
    	$userinfo = $user->getOne($userid);        
    	$Prform = new User_Form_Privacy();
    	$this->view->privacyform = $Prform;          
    }



    public function signupAction() 
    {
        $this->_layout = Zend_Layout::getMvcInstance();
        $this->_layout->setLayout('signup');
        $form = new User_Form_FreeSignup();
        $this->view->signupform = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if($this->getRequest()->getParam('ajax')=='signup')
            {
                $formData = serialize($formData);
                //echo '<pre>';print_r($formData);die();
            }

            if ($form->isValid($formData)) {
                $user = new User_Model_DbTable_User();
                $formData = $form->getValidValues($formData);
                $formData['role_id'] = 2;
                $formData['created_date'] = date('Y-m-d H:i:s');
                unset ($formData['submit']);
                unset ($formData['password_confirm']);
                $userid = $user->save($formData);
                $this->RedirectUrl('/user/login');        
                
            }
        }
    }

    
    public function dashboardAction()//Home page after login
    { 
        //echo '<pre>';print_r($this->_session->trialdays);die();
        $this->_layout = Zend_Layout::getMvcInstance();
        $this->_layout->setLayout('layout');

        
    }

    public function _setTab()
    {
        $navigation = Zend_Registry::get('topnav');
        $navigation->current()->setActive(true)->setClass('active');
        $this->view->navigation($navigation);
        
    }    

}
